package njClass.UI
{
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class PlusRadio extends Sprite
	{
		private var Radio_mc:Sprite=new Sprite();
		private var Teams:Array=new Array();
		private var txts:Array=new Array();
		private var Values:Array=new Array();
		private var currentID:int;
		private var kickIndex:int=1;
		
		public function PlusRadio(values:Array,Txts:Array=null,defaultID:int=-999,betweenBtnAndTxt:Number=20,betweenOntByOne:int=30)
		{
			Values=values;
			txts=Txts;
			trace(defaultID);
			if(defaultID==-999){
				currentID=0;
			}else{
				currentID=Values.indexOf(defaultID);
			}
			for(var i:int=0;i<values.length;i++){
				var button_mc:Sprite=new Sprite();
				button_mc.addChild(doDraw(16,16,0xCCCCCC));
				button_mc.y=betweenOntByOne*i;
				if(Txts!=null){
					var txt:TextField=new TextField();
					txt.selectable=false;
					txt.defaultTextFormat=new TextFormat("宋体",12);
					txt.width=50;
					txt.text=txts[i];
					txt.autoSize=TextFieldAutoSize.CENTER;
					txt.height=16;
					txt.y=7;
					txt.x=betweenBtnAndTxt-10;
					var mask_mc:SimpleButton=new SimpleButton();
					mask_mc.hitTestState=doDraw(50,30,0xCCCCCC);
					mask_mc.upState=mask_mc.overState=mask_mc.downState=txt;
					mask_mc.y=-7;
					mask_mc.x=16;
					button_mc.addChild(mask_mc);
					kickIndex=2;
				}
				Teams.push(button_mc);
				Radio_mc.addChild(button_mc);
			}
			Teams[currentID].addChild(drawTick.createTick(16,16));
			addChild(Radio_mc);
			
			Radio_mc.addEventListener(MouseEvent.MOUSE_OVER,MouseOver);
			Radio_mc.addEventListener(MouseEvent.CLICK,ChoosePoint);
		}
		
		private function ChoosePoint(e:MouseEvent):void{
			if(e.target is SimpleButton){
				var Obj:Sprite=Sprite(e.target.parent)
			}else{
				Obj=Sprite(e.target);
			}
			if(currentID==Teams.indexOf(Obj)){
				return;
			}
			Obj.addChild(drawTick.createTick(16,16));
			Teams[currentID].removeChildAt(kickIndex);
			currentID=Teams.indexOf(Obj);
			dispatchEvent(new Event("SliderChange"));
		}
		
		public function get value():Number{
			return Values[currentID];
		}
		
		public function set value(num:Number):void{
			if(currentID==Values.indexOf(num)){
				return;
			}
			Teams[Values.indexOf(num)].addChild(drawTick.createTick(16,16));
			Teams[currentID].removeChildAt(kickIndex);
			currentID=Values.indexOf(num);
			dispatchEvent(new Event("SliderChange"));
		}
		
		public function get text():String{
			return txts[currentID];
		}
		
		private function MouseOver(e:MouseEvent):void{
			if(e.target is SimpleButton){
				var Obj:Sprite=Sprite(e.target.parent)
			}else{
				Obj=Sprite(e.target);
			}
			var num:int=Teams.indexOf(Obj);
			Obj.removeChildAt(0);
			Obj.addChildAt(doDraw(16,16,0xCCCCCC,0xFF3300),0);
			
			Radio_mc.addEventListener(MouseEvent.MOUSE_OUT,MouseOut);
			
			function MouseOut(event:MouseEvent):void{
				Obj.removeChildAt(0);
				Obj.addChildAt(doDraw(16,16,0xCCCCCC),0);
			}
		}
		
		private function doDraw(w:Number,h:Number,bgColor:uint,lineColor:uint=0x000000):Shape{
			var temp:Shape=new Shape();
			temp.graphics.beginFill(bgColor);
			temp.graphics.lineStyle(2,lineColor,0.6,true);
			temp.graphics.drawRoundRect(0,0,w,h,5);
			temp.graphics.endFill();
			return temp;
		}
	}
}